package com.biqee.util;

import java.io.File;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class TaobaoAccessUtil {
	/*
	 * <div class="w950">
    <h1><a href="http://www.taobao.com/"><img width="140" height="35" src="http://a.tbcdn.cn/tbsp/img/header/logo.png" alt="淘宝网 Taobao.com - 阿里巴巴旗下网站"></a></h1>
    <div class="main">
        <div class="notice">您好:
        <p>我们的系统检测到您所在的网络对淘宝网的访问请求过于频繁。很抱歉，现在暂时无法处理您的要求。请您等待一段时间后再来访问。感谢您的支持。</p>
        </div>
    </div>
    <div class="safe">您的计算机可能存在安全问题，请访问<a href="http://www.taobao.com/go/chn/safe/911.php">淘宝网安全软件中心</a>。</div>
		</div>
	 */
	public static boolean isBlocked(Document doc) {
		if(doc == null) {
			throw new RuntimeException("doc should not be null");
		}
		
		Element mainDivElement = doc.select("div.main").first();
		if(mainDivElement == null) {
			return false;
		}
		
		Element noticeDivElement = mainDivElement.select("div.notice").first();
		if(noticeDivElement == null) {
			return false;
		}
		
		return true;
	}
	
	public static boolean isBlocked(String htmlString) {
		if(htmlString == null) {
			throw new RuntimeException("htmlString should not be null");
		}
		
		Document doc = JsoupUtil.createDocument(htmlString);
		return isBlocked(doc);
	}
	
	
	public static void main(String[] args) throws Exception {
		String filePath = "D:\\work\\biqee\\test-pages\\taobao\\blocked.html";
		File file = new File(filePath);
		Document doc = Jsoup.parse(file, "gbk");

		boolean isBlocked = TaobaoAccessUtil.isBlocked(doc);
		if(isBlocked) {
			System.out.println("success");
		} else {
			System.out.println("failed!");
		}
	}
	
}
